package com.candy.control;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ShowController {

    @RequestMapping("/show1.do")
    public ModelAndView show1(){
//        创建视图模型
        ModelAndView mv=new ModelAndView();
        mv.addObject("msg","SpringMVC的第一个程序");
        mv.setViewName("welcome");
        return mv;
    }
//    ResponseBody将方法返回的内容以json字符串的方式返回
    @RequestMapping("/show2.do")
    @ResponseBody
    public List<Map<String,Object>> show2(){
        List<Map<String,Object>> lm=new ArrayList<>();
        Map<String,Object>  mp=new HashMap<>();
        mp.put("username","tom");
        mp.put("age",22);
        lm.add(mp);
        return lm;
    }
    //返回值类型为String时，默认返回的内容为视图的名字
    @RequestMapping("/show3.do")
    public String show3(){
        return "welcome";
    }

    @RequestMapping("/show4.do")
    public String show4(Model model){
        model.addAttribute("msg","SpringMVC代码测试");
        return "welcome";
    }


    @RequestMapping("/show5.do")
    public String show5(@RequestParam("username") String username,@RequestParam("userpwd") String userpwd,Model model){
        System.out.println("获取到的username："+username);
        System.out.println("获取到的userpwd："+userpwd);
        model.addAttribute("msg",username);
        return "welcome";
    }


}
